package org.huoran.sakamichi.main.controller;

import org.huoran.sakamichi.main.activemq.producer.MessageType;
import org.huoran.sakamichi.main.activemq.producer.ProxyMessageTemplate;
import org.huoran.sakamichi.main.util.ResultBean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;


@Controller
@RequestMapping({"/active"})
public class ActivemqController {

    @Resource
    private ProxyMessageTemplate proxyMessageTemplate;

    private String queueName = "sakamichi-message-queue";
    private String topicName = "sakamichi-message-topic";

    @ResponseBody
    @RequestMapping(value = {"/sendByQueue/{message}"})
    public ResultBean sendQueueByPool(@PathVariable String message) {
        try{
            this.proxyMessageTemplate.sendMessage(queueName,message);
            return ResultBean.success("send success");
        } catch (Exception e) {
            e.printStackTrace();
            return ResultBean.failed("send failed");
        }
    }

    @ResponseBody
    @RequestMapping(value = {"/sendByTopic/{message}"})
    public ResultBean sendTopicByPool(@PathVariable String message) {
        try{
            this.proxyMessageTemplate.sendMessage(topicName,false, MessageType.TOPIC,message);
            return ResultBean.success("send success");
        } catch (Exception e) {
            e.printStackTrace();
            return ResultBean.failed("send failed");
        }
    }

}
